<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:exsl='http://exslt.org/common'>
    
    <xsl:output method="xml" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" version="1.0" encoding="utf-8" indent="yes" />
    <xsl:param name="project_title" />
    
    <xsl:key name="case_ids" match="case" use="@ixBug" />

    <xsl:template match="/">
    	<!-- set up the HTML document -->
    	<xsl:element name="html">
    		<xsl:attribute name="lang">en</xsl:attribute>
    		<xsl:attribute name="xml:lang">en</xsl:attribute>
    		<xsl:element name="head">
                <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                <xsl:element name="meta">
                	<xsl:attribute name="http-equiv">content-type</xsl:attribute>
                	<xsl:attribute name="content">text/html; charset=utf-8</xsl:attribute>
                </xsl:element>
                <xsl:element name="title">
                    <xsl:attribute name="id">page_title</xsl:attribute>
                	<xsl:value-of select="$project_title" disable-output-escaping="no" />
                </xsl:element>
                <xsl:element name="style">
                	<xsl:attribute name="type">text/css</xsl:attribute>
                	<![CDATA[
    body
    {
    	font-family: Tahoma, Optima, Arial, Helvetica, sans-serif;
    	font-size: 10pt;
    	background-color: #B2B2B2;
    }
    
    div#body
    {
        width: 660px;
        text-align: left;
        margin-left: auto;
        margin-right: auto;
        background-color: white;
        padding: 0.5em 1em;
    }
    
    table.case
    {
        border: 1px solid black;
        width: 100%;
        margin-bottom: 1em;
    }
    
    table.event
    {
        font-size: 0.8em;
    }
    
    table.case td
    {
        padding: 1em;
        vertical-align: top;
    }
    
    table.event td
    {
        padding: 0.5em;
        vertical-align: top;
    }
    
    div.header
    {
        border-bottom: 1px solid black;
        height: 75px;
    }
    
    div.case-number
    {
        color: rgb(22,150,132);
        font-size: 3.5em;
        font-weight: bold;
        text-align: center;
    }
    
    div.case-number a
    {
        text-decoration: none;
        color: rgb(22,150,132);
    }
    
    div.case-title
    {
        font-size: 1.5em;
        font-family: Georgia, Times, 'Times New Roman', serif;
    }
    
    div.event-title
    {
        font-weight: bold;
    }
    
    div.event-body
    {
    	padding-left: 2em;
    }
    
    div.email
    {
        margin-top: 0.5em;
        border: 1px solid black;
    }
    
    div.email-header
    {
    	background-color: #DFDFFF;
    	border-bottom: 1px #D9D9D9 solid;
    	padding: 0.5em;
    }
    
    div.email-header td
    {
        padding: 1px;
    }
    
    div.email-body
    {
        padding: 0.5em;
    }
    
    tr.menu-bar
    {
        background-color: #dedede;
    }
    
    tr.menu-bar td
    {
        text-align: right;
        padding: 2px 1em 2px;
    }
    
    tr.menu-bar a:link
    {
        text-decoration: none;
        font-weight: bold;
        color: brown;
    }
    
    tr.menu-bar a:hover
    {
        background-color: white;
    }
    
    .dimmed
    {
    	color: #808080;
    	font-weight: normal;
    }
    
    dl
    {
        font-size: 0.8em;
    }
    
    dt
    {
        font-weight: bold;
    }
    
    dd
    {
        margin-bottom: 0.5em;
    }
]]>
                </xsl:element>
                <xsl:element name="script">
                	<xsl:attribute name="type">text/javascript</xsl:attribute>
                	<![CDATA[
                	// function to update page title when internal anchor is followed
                	// so page title reflects selected case
                	var pagetitle = document.getElementById('page_title').value;
                	]]>
                </xsl:element>
    		</xsl:element>
    		<xsl:element name="body">
    		    <xsl:element name="div">
    		    	<xsl:attribute name="id">body</xsl:attribute>
    		    	<xsl:element name="h1">
    		    		<xsl:text disable-output-escaping="yes">Open Cases for </xsl:text>
    		    		<xsl:value-of select="$project_title" disable-output-escaping="no" />
    		    	</xsl:element>
    		    	<xsl:choose>
    		    		<xsl:when test="/response/cases/@count &gt; 0">
                			<xsl:apply-templates select="response/cases/case" />
    		    		</xsl:when>
    		    		<xsl:otherwise>
    		    			There are no open cases for this project
    		    		</xsl:otherwise>
    		    	</xsl:choose>
    		    </xsl:element>
    		</xsl:element>
    	</xsl:element>
    </xsl:template>
    
    <xsl:template match="case">
        <xsl:element name="table">
            <xsl:attribute name="border">0</xsl:attribute>
            <xsl:attribute name="cellpadding">0</xsl:attribute>
            <xsl:attribute name="cellspacing">0</xsl:attribute>
            <xsl:attribute name="class">case</xsl:attribute>
            <xsl:attribute name="id">c<xsl:value-of select="@ixBug" disable-output-escaping="yes" /></xsl:attribute>
            <xsl:call-template name="menu-bar" />
            <xsl:element name="tr">
            	<xsl:element name="td">
            		<!-- case number -->
            		<xsl:attribute name="style">width:170px;</xsl:attribute>
            		<xsl:element name="div">
            			<xsl:attribute name="class">header</xsl:attribute>
            			<xsl:element name="div">
            				<xsl:attribute name="class">case-number</xsl:attribute>
            				<xsl:element name="a">
            					<xsl:attribute name="href">
                                    <xsl:text disable-output-escaping="yes">#c</xsl:text>
                    				<xsl:value-of select="@ixBug" disable-output-escaping="yes" />
            					</xsl:attribute>
                				<xsl:value-of select="@ixBug" disable-output-escaping="yes" />
            				</xsl:element>
            			</xsl:element>
            		</xsl:element>
            		<xsl:element name="p">
                        <xsl:choose>
                            <xsl:when test="ixCategory = 1">
                                <!-- bug -->
                                <xsl:attribute name="style">color:red;font-style:italic</xsl:attribute>
                            </xsl:when>
                            <xsl:when test="ixCategory = 2">
                                <!-- feature -->
                                <xsl:attribute name="style">color:green;font-style:italic</xsl:attribute>
                            </xsl:when>
                            <xsl:when test="ixCategory = 3">
                                <!-- inquiry -->
                                <xsl:attribute name="style">color:gray;</xsl:attribute>
                            </xsl:when>
                            <xsl:otherwise>
                                <!-- schedule item -->
                            </xsl:otherwise>
                        </xsl:choose>
                        <xsl:value-of select="sCategory" disable-output-escaping="yes" />
            		</xsl:element>
            		<xsl:element name="dl">
            			<xsl:element name="dt">
            			    <xsl:text disable-output-escaping="yes">Priority</xsl:text>
            			</xsl:element>
                        <xsl:element name="dd">
                            <xsl:value-of select="ixPriority" disable-output-escaping="yes" />
                            <xsl:text disable-output-escaping="yes"> - </xsl:text>
                            <xsl:value-of select="sPriority" disable-output-escaping="yes" />
                        </xsl:element>
                        <xsl:if test="count(hrsOrigEst) &gt; 0">
                            <xsl:element name="dt">
                                <xsl:text disable-output-escaping="yes">Estimate</xsl:text>
                            </xsl:element>
                            <xsl:element name="dd">
                        	    <xsl:value-of select="hrsOrigEst" disable-output-escaping="yes" />
                        	    <xsl:text disable-output-escaping="yes"> hour(s)</xsl:text>
                            </xsl:element>
                        </xsl:if>
                        <xsl:if test="ixRelatedBugs != ''">
                            <xsl:element name="dt">
                                <xsl:text disable-output-escaping="yes">Related Bugs</xsl:text>
                            </xsl:element>
                            <xsl:element name="dd">
                                <xsl:call-template name="linkate-related-cases" />
                            </xsl:element>
                        </xsl:if>
            		</xsl:element>
            	</xsl:element>
            	<xsl:element name="td">
            		<!-- case -->
            		<xsl:element name="div">
            		    <xsl:attribute name="class">header</xsl:attribute>
            		    <xsl:element name="div">
            		    	<xsl:attribute name="class">case-title</xsl:attribute>
            		    	<xsl:value-of select="sTitle" disable-output-escaping="no" />
            		    </xsl:element>
            		    <xsl:element name="div">
            		    	<xsl:value-of select="sArea" disable-output-escaping="yes" />
            		    	<xsl:text disable-output-escaping="yes"> </xsl:text>
            		    	<xsl:element name="span">
            		    		<xsl:attribute name="class">dimmed</xsl:attribute>
            		    		<xsl:text disable-output-escaping="yes">| Fix for </xsl:text>
            		    		<xsl:value-of select="sFixFor" disable-output-escaping="yes" />
            		    	</xsl:element>
            		    </xsl:element>
            		</xsl:element>
            		<xsl:element name="table">
            			<xsl:attribute name="border">0</xsl:attribute>
            			<xsl:attribute name="cellpadding">0</xsl:attribute>
            			<xsl:attribute name="cellspacing">0</xsl:attribute>
                        <xsl:attribute name="class">event</xsl:attribute>
                        <xsl:apply-templates select="events/event"/>
            		</xsl:element>
            	</xsl:element>
            </xsl:element>
            <xsl:call-template name="menu-bar" />
        </xsl:element>
    </xsl:template>
    
    <xsl:template match="event">
        <xsl:choose>
            <xsl:when test="normalize-space(evt) = 1">
                <!-- opened (by) -->
                <xsl:call-template name="format_event" />
            </xsl:when>
            <xsl:when test="normalize-space(evt) = 13">
                <!-- not sorted - still in training -->
                
            </xsl:when>
            <xsl:when test="normalize-space(evt) = 2">
                <!-- edited -->
                <xsl:call-template name="format_event" />
            </xsl:when>
            <xsl:when test="normalize-space(evt) = 3">
                <!-- Assigned to -->
                
            </xsl:when>
            <xsl:when test="normalize-space(evt) = 5">
                <!-- Reopened (assigned to Gustave Stresen-Reuter) -->
                <xsl:call-template name="format_event" />
            </xsl:when>
            <xsl:when test="normalize-space(evt) = 6">
                <!-- closed -->
                <xsl:call-template name="format_event" />
            </xsl:when>
            <xsl:when test="(normalize-space(evt) &gt;= 9 and normalize-space(evt) &lt;= 11) or normalize-space(evt) = 15">
                <!-- email -->
                <xsl:call-template name="format_event_as_email" />
            </xsl:when>
            <xsl:when test="normalize-space(evt) = 14">
                <!-- Resolved (Responded) -->
                <xsl:call-template name="format_event" />
            </xsl:when>
            <xsl:otherwise>
                
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    
    <xsl:template name="format_event">
        <xsl:element name="tr">
            <xsl:attribute name="id">e<xsl:value-of select="@ixBugEvent" disable-output-escaping="yes" /></xsl:attribute>
                <xsl:element name="td">
                    <xsl:element name="div">
                    	<xsl:attribute name="class">event-title</xsl:attribute>
                    	<xsl:value-of select="evtDescription" disable-output-escaping="yes" />
                    	<xsl:text disable-output-escaping="yes"> </xsl:text>
                    	<xsl:element name="span">
                    		<xsl:attribute name="class">dimmed</xsl:attribute>
                    		<xsl:value-of select="dt" disable-output-escaping="yes" />
                    	</xsl:element>
                    </xsl:element>
                    <xsl:element name="div">
                    	<xsl:attribute name="class">event-body</xsl:attribute>
                    	<xsl:if test="sChanges != ''">
                    		<xsl:element name="div">
                    			<xsl:attribute name="style">white-space: pre-line; margin-left: 2em;</xsl:attribute>
                    			<xsl:value-of select="sChanges" disable-output-escaping="yes" />
                    		</xsl:element>
                    	</xsl:if>
                    	<xsl:if test="s != ''">
                    		<xsl:element name="p">
                    			<xsl:attribute name="style">white-space: pre-line; font-size: 1em;</xsl:attribute>
                    			<xsl:value-of select="s" disable-output-escaping="yes" />
                    		</xsl:element>
                    	</xsl:if>
                    </xsl:element>
                </xsl:element>
        </xsl:element>
    </xsl:template>
    
    
    
    <xsl:template name="format_event_as_email">
        <xsl:element name="tr">
            <xsl:attribute name="id">e<xsl:value-of select="@ixBugEvent" disable-output-escaping="yes" /></xsl:attribute>
                <xsl:element name="td">
                    <xsl:element name="div">
                    	<xsl:attribute name="class">event-title</xsl:attribute>
                    	<xsl:value-of select="evtDescription" disable-output-escaping="yes" />
                    	<xsl:text disable-output-escaping="yes"> </xsl:text>
                    	<xsl:element name="span">
                    		<xsl:attribute name="class">dimmed</xsl:attribute>
                    		<xsl:value-of select="dt" disable-output-escaping="yes" />
                    	</xsl:element>
                    </xsl:element>
                    <xsl:element name="div">
                    	<xsl:attribute name="class">email</xsl:attribute>
                    	<xsl:element name="div">
                    		<xsl:attribute name="class">email-header</xsl:attribute>
                    		    <xsl:element name="table">
                    		    	<xsl:attribute name="border">0</xsl:attribute>
                    		    	<xsl:attribute name="cellpadding">0</xsl:attribute>
                    		    	<xsl:attribute name="cellspacing">0</xsl:attribute>
                    		    	<xsl:if test="sFrom != ''">
                    		    		<xsl:element name="tr">
                    		    			<xsl:element name="td">
                    		    			    <xsl:element name="b">
                    		    			    	<xsl:text disable-output-escaping="yes">From:</xsl:text>
                    		    			    </xsl:element>
                    		    			</xsl:element>
                    		    			<xsl:element name="td">
                    		    				<xsl:value-of select="sFrom" disable-output-escaping="no" />
                    		    			</xsl:element>
                    		    		</xsl:element>
                    		    	</xsl:if>
                    		    	<xsl:if test="sReplyTo != ''">
                    		    		<xsl:element name="tr">
                    		    			<xsl:element name="td">
                    		    			    <xsl:element name="b">
                    		    			    	<xsl:text disable-output-escaping="yes">Reply-To:</xsl:text>
                    		    			    </xsl:element>
                    		    			</xsl:element>
                    		    			<xsl:element name="td">
                    		    				<xsl:value-of select="sReplyTo" disable-output-escaping="no" />
                    		    			</xsl:element>
                    		    		</xsl:element>
                    		    	</xsl:if>
                    		    	<xsl:if test="sDate != ''">
                    		    		<xsl:element name="tr">
                    		    			<xsl:element name="td">
                    		    			    <xsl:element name="b">
                    		    			    	<xsl:text disable-output-escaping="yes">Date:</xsl:text>
                    		    			    </xsl:element>
                    		    			</xsl:element>
                    		    			<xsl:element name="td">
                    		    				<xsl:value-of select="sDate" disable-output-escaping="no" />
                    		    			</xsl:element>
                    		    		</xsl:element>
                    		    	</xsl:if>
                    		    	<xsl:if test="sTo != ''">
                    		    		<xsl:element name="tr">
                    		    			<xsl:element name="td">
                    		    			    <xsl:element name="b">
                    		    			    	<xsl:text disable-output-escaping="yes">To:</xsl:text>
                    		    			    </xsl:element>
                    		    			</xsl:element>
                    		    			<xsl:element name="td">
                    		    				<xsl:value-of select="sTo" disable-output-escaping="no" />
                    		    			</xsl:element>
                    		    		</xsl:element>
                    		    	</xsl:if>
                    		    	<xsl:if test="sCc != ''">
                    		    		<xsl:element name="tr">
                    		    			<xsl:element name="td">
                    		    			    <xsl:element name="b">
                    		    			    	<xsl:text disable-output-escaping="yes">Cc:</xsl:text>
                    		    			    </xsl:element>
                    		    			</xsl:element>
                    		    			<xsl:element name="td">
                    		    				<xsl:value-of select="sCc" disable-output-escaping="no" />
                    		    			</xsl:element>
                    		    		</xsl:element>
                    		    	</xsl:if>
                    		    	<xsl:if test="sBcc != ''">
                    		    		<xsl:element name="tr">
                    		    			<xsl:element name="td">
                    		    			    <xsl:element name="b">
                    		    			    	<xsl:text disable-output-escaping="yes">Bcc:</xsl:text>
                    		    			    </xsl:element>
                    		    			</xsl:element>
                    		    			<xsl:element name="td">
                    		    				<xsl:value-of select="sBcc" disable-output-escaping="no" />
                    		    			</xsl:element>
                    		    		</xsl:element>
                    		    	</xsl:if>
                    		    	<xsl:if test="sSubject != ''">
                    		    		<xsl:element name="tr">
                    		    			<xsl:element name="td">
                    		    			    <xsl:element name="b">
                    		    			    	<xsl:text disable-output-escaping="yes">Subject:</xsl:text>
                    		    			    </xsl:element>
                    		    			</xsl:element>
                    		    			<xsl:element name="td">
                    		    				<xsl:value-of select="sSubject" disable-output-escaping="no" />
                    		    			</xsl:element>
                    		    		</xsl:element>
                    		    	</xsl:if>
                    		    </xsl:element>
                    	</xsl:element>
                    	<xsl:element name="div">
                    		<xsl:attribute name="class">email-body</xsl:attribute>
                    		<xsl:value-of select="sBodyHTML" disable-output-escaping="yes" />
                    	</xsl:element>
                    </xsl:element>
                </xsl:element>
        </xsl:element>
    </xsl:template>
    
    
    
    <xsl:template name="menu-bar">
        <xsl:element name="tr">
            <xsl:attribute name="class">menu-bar</xsl:attribute>
            <xsl:element name="td">
                <xsl:attribute name="colspan">2</xsl:attribute>
                <xsl:element name="a">
                    <xsl:attribute name="href">mailto:support%40chicagoitsystems.com?subject=Re:%20(Case%20<xsl:value-of select="@ixBug" disable-output-escaping="yes" />)</xsl:attribute>
                    <xsl:text disable-output-escaping="yes">Reply</xsl:text>
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>
    
    
    
    <xsl:template name="linkate-related-cases">
        <xsl:variable name="ids">
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="string" select="ixRelatedBugs" />
                    <xsl:with-param name="delimiters" select="','" />
                </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="matched_ids">
        	<xsl:copy-of select="key('case_ids',exsl:node-set($ids)/token)" />
        </xsl:variable>
        <xsl:for-each select="exsl:node-set($ids)/token">
            <xsl:variable name="curr_id" select="." />
            <xsl:choose>
            	<xsl:when test="count(exsl:node-set($matched_ids)/case[@ixBug = $curr_id]) &gt; 0">
                    <xsl:element name="a">
                        <xsl:attribute name="href">#c<xsl:value-of select="." disable-output-escaping="yes" /></xsl:attribute>
                        <xsl:value-of select="." disable-output-escaping="yes" />
                    </xsl:element>
            	</xsl:when>
            	<xsl:otherwise>
            		<xsl:value-of select="." disable-output-escaping="yes" />
            	</xsl:otherwise>
            </xsl:choose>
        	<xsl:if test="position() != last()">
        		<xsl:text disable-output-escaping="yes">, </xsl:text>
        	</xsl:if>
        </xsl:for-each>
    </xsl:template>
    
    
    <xsl:template match="node() | @* | comment() | processing-instruction()">
		<xsl:apply-templates select="@* | node()"/>
    </xsl:template>
    
    
    <!-- Utilities -->
    <xsl:template name="tokenize">
    	<xsl:param name="string" select="''"/>
    	<xsl:param name="delimiters" select="' &#x9;&#xA;'"/>
    	<xsl:choose>
    	    <!-- Nothing to do if empty string -->
    		<xsl:when test="not($string)" />
    	    <!-- No delimiters signals character level tokenization -->
    		<xsl:when test="not($delimiters)">
    		    <xsl:call-template name="_tokenize-characters">
    		    	<xsl:with-param name="string" select="$string"/>
    		    </xsl:call-template>
    		</xsl:when>
    		<xsl:otherwise>
    			<xsl:call-template name="_tokenize-delimiters">
    				<xsl:with-param name="string" select="$string"/>
    				<xsl:with-param name="delimiters" select="$delimiters"/>
    			</xsl:call-template>
    		</xsl:otherwise>
    	</xsl:choose>
    </xsl:template>
    
    <xsl:template name="_tokenize-characters">
    	<xsl:param name="string" />
    	<xsl:if test="$string">
    	    <token><xsl:value-of select="substring($string, 1, 1)" /></token>
    	    <xsl:call-template name="_tokenize-characters">
    	    	<xsl:with-param name="string" select="substring($string, 2)"/>
    	    </xsl:call-template>
    	</xsl:if>
    </xsl:template>
    
    <xsl:template name="_tokenize-delimiters">
    	<xsl:param name="string" />
    	<xsl:param name="delimiters" />
    	<xsl:param name="last-delimit" />
    	<!-- Extract a delimiter -->
    	<xsl:variable name="delimiter" select="substring($delimiters, 1, 1)" />
    	<xsl:choose>
    	    <!-- If the delimiter is empty, we have a token -->
    		<xsl:when test="not($delimiter)">
    			<token><xsl:value-of select="$string" /></token>
    		</xsl:when>
    		<!-- If the string contains at least one delimiter we must split it -->
    		<xsl:when test="contains($string, $delimiter)">
    			<!-- If it starts with a delim, we don't need to handle the before part -->
    			<xsl:if test="not(starts-with($string, $delimiter))">
    				<!-- Handle the part that comes before the current delimiter -->
    				<!-- with the next delimiter. If there is no next the first test -->
    				<!-- in this template will detect the token -->
    				<xsl:call-template name="_tokenize-delimiters">
    					<xsl:with-param name="string" select="substring-before($string, $delimiter)"/>
    					<xsl:with-param name="delimiters" select="substring($delimiters, 2)"/>
    				</xsl:call-template>
    			</xsl:if>
    			<!-- Handle the part that comes after the delimiter using the -->
    			<!-- current delimiter -->
    			<xsl:call-template name="_tokenize-delimiters">
    				<xsl:with-param name="string" select="substring-after($string, $delimiter)"/>
    				<xsl:with-param name="delimiters" select="$delimiters"/>
    			</xsl:call-template>
    		</xsl:when>
    		<xsl:otherwise>
    			<!-- No occurances of current delimiter so move on to next -->
    			<xsl:call-template name="_tokenize-delimiters">
    				<xsl:with-param name="string" select="$string"/>
    				<xsl:with-param name="delimiters" select="substring($delimiters, 2)"/>
    			</xsl:call-template>
    		</xsl:otherwise>
    	</xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>